package com.zzjc.webauth.controller;

import com.zzjc.util.request.RequestUtils;
import com.zzjc.webauth.entity.User;
import com.zzjc.webauth.service.UserService;
import com.zzjc.webauth.utils.ApiResponse;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Email;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理接口")
public class UserController {


    private static final Logger logger = LoggerFactory.getLogger(RequestUtils.class);
    @Autowired
    private UserService userService;


    @ApiOperation("用户注册")
    @PostMapping("/register")
    @ApiImplicitParams({@ApiImplicitParam(name = "userAcct", value = "手机号(账号)", dataType = "String", required = true),
            @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", required = true),
            @ApiImplicitParam(name = "email", value = "邮箱", dataType = "String", required = false),
            @ApiImplicitParam(name = "roleType", value = "1: admin, 2: 普通用户", dataType = "Int", required = true),
            @ApiImplicitParam(name = "gender", value = "性别 1:男,2:女", dataType = "Int", required = false),
    })
    public ApiResponse register(@RequestParam(value = "userAcct", required = true) String userAcct,
                                @RequestParam(value = "username", required = true) String username,
                                @RequestParam(value = "password", required = true) String password,
                                @RequestParam(value = "email", required = false) @Email String email,
                                @RequestParam(value = "roleType", required = true) Integer roleType,
                                @RequestParam(value = "gender", required = false) Integer gender) {
        User user = new User();
        user.setUserAcct(userAcct);
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setRoleType(roleType);
        user.setGender(gender);
        return userService.register(user);
    }


    @ApiOperation("删除用户")
    @PostMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userList", value = "用户id集合", dataType = "String", required = true)
    })
    public ApiResponse deleteUser(
            @RequestParam(value = "userList", required = true) List<String> userList) {
        return userService.deleteUser(userList);
    }

    @ApiOperation("用户信息查询")
    @PostMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "userAcct", value = "手机号(账号)", dataType = "String", required = false),
            @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", required = false),
            @ApiImplicitParam(name = "roleType", value = "1: admin, 2: 普通用户", dataType = "Int", required = false),
            @ApiImplicitParam(name = "gender", value = "性别 1:男,2:女", dataType = "Int", required = false),
            @ApiImplicitParam(name = "pageNum", value = "当前页index", dataType = "Int", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页面数据条数", dataType = "Int", required = true)
    })
    public ApiResponse<User> listUserInfo(
            @RequestParam(value = "userAcct", required = false) String userAcct,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "roleType", required = false) Integer roleType,
            @RequestParam(value = "gender", required = false) Integer gender,
            @RequestParam(value = "pageNum", required = true) Integer pageNum,
            @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        User user = new User();
        user.setUserAcct(userAcct);
        user.setUsername(username);
        user.setRoleType(roleType);
        user.setGender(gender);
        user.setPageNum(pageNum);
        user.setPageSize(pageSize);
        return userService.listUser(user);
    }

    @ApiOperation("用户信息更新")
    @PostMapping("/update")
    @ApiImplicitParams({@ApiImplicitParam(name = "userAcct", value = "手机号(账号)", dataType = "String", required = true),
            @ApiImplicitParam(name = "email", value = "邮箱", dataType = "String", required = false),
            @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", required = false),
            @ApiImplicitParam(name = "roleType", value = "1: admin, 2: 普通用户", dataType = "Int", required = false),
            @ApiImplicitParam(name = "gender", value = "性别 1:男,2:女", dataType = "Int", required = false),

    })
    public ApiResponse<User> updateUserInfo(
            @RequestParam(value = "userAcct", required = true) String userAcct,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "roleType", required = false) Integer roleType,
            @RequestParam(value = "gender", required = false) Integer gender,
            @RequestParam(value = "email", required = false) String email
            ) {
        User user = new User();
        user.setUserAcct(userAcct);
        user.setUsername(username);
        user.setRoleType(roleType);
        user.setGender(gender);
        user.setEmail(email);
        return userService.updateUser(user);
    }



}
